﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI;
using System.Data;

namespace BEGDC.CC.Web.Workflow.Common
{
    public class EntityUtility
    {
        public EntityUtility()
        {

        }
        /// <summary>
        /// 赋值到控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="val"></param>
        protected static void DataBindToControl(System.Web.UI.Control control, object val)
        {
            if (val == null)
            {
                val = "";
            }
            if (val is DateTime)
            {
                val = ((DateTime)val).ToString("yyyy-MM-dd");
            }
            if (control != null)
            {
                if (control is TextBox)
                {
                    (control as TextBox).Text = val.ToString();
                    if (val is DateTime)
                    {
                        (control as TextBox).Text = ((DateTime)val).ToString("yyyy-MM-dd");
                    }
                }
                else if (control is Label)
                {
                    Label labela = control as Label;
                    if (val is bool)
                    {
                        (control as Label).Text = ((bool)val) ? "True" : "False";
                    }
                    else
                    {
                        (control as Label).Text = val.ToString();
                    }
                }
                else if (control is CheckBox)
                {
                    if ((val is string) && string.IsNullOrEmpty(val as string)) return;
                    (control as CheckBox).Checked = (bool)Convert.ChangeType(val, typeof(bool));
                }
                else if (control is DropDownList)
                {
                    if ((control as DropDownList).Items.Count > 0)
                    {
                        if (val is bool)
                        {
                            (control as DropDownList).SelectedValue = ((bool)val) ? "1" : "0";
                        }
                        else
                        {
                            (control as DropDownList).SelectedValue = val.ToString();
                        }
                    }
                }
                else if (control is RadioButtonList)
                {
                    if ((control as RadioButtonList).Items.Count > 0)
                    {
                        if (val is bool)
                        {
                            (control as RadioButtonList).SelectedValue = ((bool)val) ? "1" : "0";
                        }
                        else
                        {
                            (control as RadioButtonList).SelectedValue = val.ToString();
                        }
                    }
                }
                //else if(control is UserControl)
                //{
                //    foreach (Control subControl in control.Controls)
                //    {
                //        if (subControl is DropDownList)
                //        {
                //            DataBindToControl(subControl, val);
                //        }
                //    }
                //}
                else if (control.GetType().Name.ToLower() == "usercontrols_combobox_ascx")
                {
                    System.Web.UI.Control combox = control.FindControl("cboMain");
                    if (combox != null)
                    {
                        if (val is bool)
                        {
                            (combox as DropDownList).SelectedValue = ((bool)val) ? "1" : "0";
                        }
                        else
                        {
                            (combox as DropDownList).SelectedValue = val.ToString();
                        }
                    }
                }
                else if (control.GetType().Name.ToLower() == "usercontrols_calendar_ascx")
                {
                    System.Web.UI.Control dateInput = control.FindControl("Raddatepicker");
                    if (dateInput != null)
                    {
                        if (val.ToString() != "")
                            (dateInput as RadDatePicker).SelectedDate = Convert.ToDateTime(val.ToString());
                    }
                }
                else if (control.GetType().Name.ToLower() == "usercontrols_calendarcontrol_ascx")
                {
                    System.Web.UI.Control dateInput = control.FindControl("Raddatepicker");
                    if (dateInput != null)
                    {
                        if (val.ToString() != "")
                            (dateInput as TextBox).Text = val.ToString();
                    }
                }

            }
        }
        public static void FillDataToControls(object entity, Page page)
        {

            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {

                foreach (PropertyInfo property in properties)
                {
                    System.Web.UI.Control control = page.FindControl("__" + property.Name);
                    if (control == null)
                    {
                        control = page.FindControl("lbl__" + property.Name);
                    }
                    if (control == null) continue;
                    object val = property.GetValue(entity, null);
                    DataBindToControl(control, val);

                }
            }
        }
        public static void FillDataToControls(object entity, WebControl controlContainer)
        {

            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {

                foreach (PropertyInfo property in properties)
                {
                    System.Web.UI.Control control = controlContainer.FindControl("__" + property.Name);
                    System.Web.UI.Control readcontrol = controlContainer.FindControl("lbl__" + property.Name);
                    if (control == null && readcontrol == null) continue;
                    object val = property.GetValue(entity, null);
                    DataBindToControl(control, val);
                    DataBindToControl(readcontrol, val);
                }
            }
        }
        public static void FillDataToLabelControls(object entity, WebControl controlContainer)
        {

            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {

                foreach (PropertyInfo property in properties)
                {
                    System.Web.UI.Control readcontrol = controlContainer.FindControl("lbl__" + property.Name);
                    if (readcontrol == null) continue;
                    object val = property.GetValue(entity, null);
                    DataBindToControl(readcontrol, val);
                }
            }
        }
        public static void FillDataToControls(object entity, UpdatePanel controlContainer)
        {

            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {

                foreach (PropertyInfo property in properties)
                {
                    System.Web.UI.Control control = controlContainer.FindControl("__" + property.Name);
                    if (control == null)
                    {
                        control = controlContainer.FindControl("lbl__" + property.Name);
                    }
                    if (control == null) continue;
                    object val = property.GetValue(entity, null);
                    DataBindToControl(control, val);
                }
            }
        }
        public static void FillDataToControls(DataTable dt, Page page)
        {
            if (dt == null) return;
            if (dt.Rows.Count == 0) return;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                System.Web.UI.Control control = page.FindControl("__" + dt.Columns[i].ColumnName);
                if (control == null)
                {
                    control = page.FindControl("lbl__" + dt.Columns[i].ColumnName);
                }
                object val = dt.Rows[0][i] == DBNull.Value ? null : dt.Rows[0][i];
                DataBindToControl(control, val);
            }
        }
        public static void FillDataToControls(DataRow row, WebControl controlContainer)
        {

            if (row == null) return;

            for (int i = 0; i < row.Table.Columns.Count; i++)
            {
                System.Web.UI.Control control = controlContainer.FindControl("__" + row.Table.Columns[i].ColumnName);
                if (control == null)
                {
                    control = controlContainer.FindControl("lbl__" + row.Table.Columns[i].ColumnName);
                }
                object val = row[i] == DBNull.Value ? null : row[i];
                DataBindToControl(control, val);
            }
        }

        public static void FillDataToLabelControls(DataRow row, WebControl controlContainer)
        {

            if (row == null) return;

            for (int i = 0; i < row.Table.Columns.Count; i++)
            {
                System.Web.UI.Control control = controlContainer.FindControl("lbl__" + row.Table.Columns[i].ColumnName);
                object val = row[i] == DBNull.Value ? null : row[i];
                DataBindToControl(control, val);
            }
        }
        public static void FillDataToControls(DataRow row, UpdatePanel controlContainer)
        {

            if (row == null) return;

            for (int i = 0; i < row.Table.Columns.Count; i++)
            {
                System.Web.UI.Control control = controlContainer.FindControl("__" + row.Table.Columns[i].ColumnName);
                if (control == null)
                {
                    control = controlContainer.FindControl("lbl__" + row.Table.Columns[i].ColumnName);
                }
                object val = row[i] == DBNull.Value ? null : row[i];
                DataBindToControl(control, val);
            }
        }
        /// <summary>
        /// 通过控件创建实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="form"></param>
        public static void FillControlToObject(object entity, Page page)
        {
            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {
                foreach (PropertyInfo property in properties)
                {
                    System.Web.UI.Control control = page.FindControl("__" + property.Name);
                    object value = GetControlValue(control);
                    if (value != null && value.GetType() == property.PropertyType)
                    {
                        property.SetValue(entity, value, null);
                    }
                    else if ((value != null && property.PropertyType != typeof(Guid)) || (value != null && property.PropertyType != typeof(Guid) && ((Guid)value) != Guid.Empty))
                    {
                        property.SetValue(entity, Convert.ChangeType(value, GetValueType(property)), null);

                    }
                }
            }
        }


        /// <summary>
        /// 通过控件创建实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="form"></param>
        public static void FillControlToObject(object entity, WebControl controlContainer)
        {
            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {
                foreach (PropertyInfo property in properties)
                {
                    System.Web.UI.Control control = controlContainer.FindControl("__" + property.Name);
                    object value = GetControlValue(control);
                    if ((value is String) && string.IsNullOrEmpty(value == null ? "" : value.ToString()) && property.PropertyType != typeof(String))
                    {
                        value = null;
                    }
                    if (value != null && value.GetType() == property.PropertyType)
                    {
                        property.SetValue(entity, value, null);
                    }
                    else if (value != null)
                    {
                        if (GetValueType(property) == typeof(System.Guid))
                        {
                            property.SetValue(entity, new Guid(value.ToString()), null);
                        }
                        else
                        {
                            if (GetValueType(property) == typeof(System.Boolean) && value is string)
                            {
                                value = (value == "1");
                            }
                            property.SetValue(entity, Convert.ChangeType(value, GetValueType(property)), null);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 通过控件创建实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="form"></param>
        public static void FillControlToObject(object entity, UpdatePanel updatePanel)
        {
            if (entity == null) return;
            try
            {
                Type type = entity.GetType();
                PropertyInfo[] properties = type.GetProperties();
                if (properties.Length > 0)
                {
                    foreach (PropertyInfo property in properties)
                    {
                        System.Web.UI.Control control = updatePanel.FindControl("__" + property.Name);
                        object value = GetControlValue(control);
                        if ((value is String) && string.IsNullOrEmpty(value == null ? "" : value.ToString()) && property.PropertyType != typeof(String))
                        {
                            value = null;
                        }
                        if (value != null && value.GetType() == property.PropertyType)
                        {
                            property.SetValue(entity, value, null);
                        }
                        else if ((value != null))
                        {
                            if (GetValueType(property) == typeof(System.Boolean) && value is string)
                            {
                                value = (value == "1");
                            }
                            property.SetValue(entity, Convert.ChangeType(value, GetValueType(property)), null);

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }
        /// <summary>
        /// 通过DataRow 创建实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="row"></param>
        public static void FillControlToObject(object entity, DataRow row)
        {
            if (entity == null) return;
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length > 0)
            {
                foreach (PropertyInfo property in properties)
                {
                    if (row.Table.Columns.Contains(property.Name))
                    {
                        object value = row[property.Name];
                        if ((value is String) && string.IsNullOrEmpty(value == null ? "" : value.ToString()) && property.PropertyType != typeof(String))
                        {
                            value = null;
                        }
                        if (value != null && value.GetType() == property.PropertyType && value != DBNull.Value)
                        {
                            property.SetValue(entity, value, null);
                        }
                        else if ((value != null && value != DBNull.Value))
                        {
                            property.SetValue(entity, Convert.ChangeType(value, GetValueType(property)), null);

                        }
                    }
                }
            }
        }
        /// <summary>
        /// 获取控件值
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        private static object GetControlValue(System.Web.UI.Control control)
        {
            object returnValue = null;
            if (control != null)
            {
                if (control is TextBox)
                {
                    returnValue = (control as TextBox).Text.Trim();
                }
                else if (control is Label)
                {
                    Label label = control as Label;
                    returnValue = label.Text;
                }
                else if (control is CheckBox)
                {
                    returnValue = (control as CheckBox).Checked;
                }
                else if (control is DropDownList)
                {
                    returnValue = (control as DropDownList).SelectedValue;
                }
                else if (control is RadioButtonList)
                {
                    returnValue = (control as RadioButtonList).SelectedValue;
                }
                else if (control.GetType().Name.ToLower() == "usercontrols_combobox_ascx")
                {
                    System.Web.UI.Control combox = control.FindControl("cboMain");
                    if (combox != null)
                    {
                        returnValue = (combox as DropDownList).SelectedValue;
                    }
                }
                else if (control.GetType().Name.ToLower() == "usercontrols_calendar_ascx")
                {
                    System.Web.UI.Control dateInput = control.FindControl("Raddatepicker");
                    if (dateInput != null)
                    {
                        returnValue = (dateInput as RadDatePicker).SelectedDate;
                    }
                }
                else if (control.GetType().Name.ToLower() == "usercontrols_calendarcontrol_ascx")
                {
                    System.Web.UI.Control dateInput = control.FindControl("Raddatepicker");
                    if (dateInput != null)
                    {
                        returnValue = (dateInput as TextBox).Text;
                    }
                }
            }
            return returnValue;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        private static Type GetValueType(PropertyInfo property)
        {
            if (!property.PropertyType.FullName.Contains("Nullable"))
            {
                return property.PropertyType;
            }
            if (property.PropertyType.FullName.Contains("System.Int32"))
            {
                return typeof(System.Int32);
            }
            else if (property.PropertyType.FullName.Contains("System.Decimal"))
            {
                return typeof(System.Decimal);
            }
            else if (property.PropertyType.FullName.Contains("System.DateTime"))
            {
                return typeof(System.DateTime);
            }
            else if (property.PropertyType.FullName.Contains("System.Boolean"))
            {
                return typeof(System.Boolean);
            }
            else if (property.PropertyType.FullName.Contains("System.Double"))
            {
                return typeof(System.Double);
            }
            else if (property.PropertyType.FullName.Contains("System.Byte"))
            {
                return typeof(System.Byte);
            }
            else if (property.PropertyType.FullName.Contains("System.Single"))
            {
                return typeof(System.Single);
            }
            else if (property.PropertyType.FullName.Contains("System.String"))
            {
                return typeof(System.String);
            }
            else if (property.PropertyType.FullName.Contains("System.Guid"))
            {

                return typeof(System.Guid);
            }
            return property.PropertyType;
        }
    }
}
